from django.db import models
from django.conf import settings


class Story(models.Model):
    """故事模型"""
    
    STATUS_CHOICES = [
        ('draft', '草稿'),
        ('published', '已发布'),
        ('rejected', '已驳回'),
    ]
    
    REVIEW_STATUS_CHOICES = [
        ('pending', '待审核'),
        ('approved', '已通过'),
        ('rejected', '已驳回'),
    ]
    
    CATEGORY_CHOICES = [
        ('奇幻', '奇幻'),
        ('科幻', '科幻'),
        ('冒险', '冒险'),
        ('爱情', '爱情'),
        ('悬疑', '悬疑'),
        ('恐怖', '恐怖'),
        ('历史', '历史'),
        ('现代', '现代'),
        ('武侠', '武侠'),
        ('仙侠', '仙侠'),
    ]
    
    title = models.CharField(max_length=200, verbose_name='标题')
    content = models.TextField(verbose_name='内容')
    category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, verbose_name='分类')
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft', verbose_name='状态')
    
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='stories',
        verbose_name='作者'
    )
    
    tags = models.JSONField(default=list, verbose_name='标签', blank=True)
    likes = models.IntegerField(default=0, verbose_name='点赞数')
    views = models.IntegerField(default=0, verbose_name='浏览量')
    
    review_status = models.CharField(
        max_length=10, 
        choices=REVIEW_STATUS_CHOICES, 
        default='pending', 
        verbose_name='审核状态'
    )
    review_note = models.TextField(blank=True, null=True, verbose_name='审核备注')
    
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    published_at = models.DateTimeField(null=True, blank=True, verbose_name='发布时间')
    
    class Meta:
        db_table = 'stories'
        verbose_name = '故事'
        verbose_name_plural = '故事'
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['-created_at']),
            models.Index(fields=['category']),
            models.Index(fields=['status']),
        ]
    
    def __str__(self):
        return self.title
    
    @property
    def comments_count(self):
        """评论数量"""
        return self.comments.count()
